package com.ccl.iot.device;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ccl.iot.IOTNetAPIEnum.EIOT_NETWORK_TYPE;
import com.ccl.iot.R;
import com.ccl.iot.device.IOTProbe.OnRegDataChangedListener;
import com.ccl.iot.dvr.utils.CommonUtils;
import com.ccl.iot.dvr.utils.GlideUtil;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.views.BrightnessPickerView;
import com.ccl.iot.views.ColorPickerView;
import com.ccl.iot.views.ColorPickerView.OnColorChangeListener;
import com.ccl.iot.views.WaitRegView;

public class RGBBulbPickerView extends LinearLayout  {

    private Context mContext;
    private ColorPickerView mColorPicker; // 调色板
    private View mCurrentColorView; // 显示块
    private View mWhiteColorView; // 固定颜色
    private View mRedColorView;
    private View mYellowColorView;
    private View mGreenColorView;
    private View mLightBlueColorView;
    private View mBlueColorView;
    private View mPinkColorView;
    private View mBlackColorView;

    private boolean isTouch = false;

    private IOTRgbLight mDevice; // 多彩灯

    /**
     * 调色板和监控的容器
     */
    private FrameLayout mContainer;

    /**
     * 把三原色转换成设备对应的颜色值
     */
    public static final float MULTIPLES = 16.06f;

    /**
     * 把设备的颜色值转换成三原色
     */
    private static final float MULTIPLES2 = 16.05f;
    private BrightnessPickerView mBrightnessPicker;
    public ImageView mDeviceImg;
    private WaitRegView mWaitRegView;
    public LinearLayout mCameraLayout;

    public RGBBulbPickerView(Context context, IOTRgbLight dev) {
        super(context, null);
        mDevice = dev;
        mContext = context;
        init();
        initListener();
    }

    private void init() {
        View.inflate(mContext, R.layout.view_bulb_picker_color_full, this);

        mColorPicker = (ColorPickerView) findViewById(R.id.bulb_picker_view);
        mBrightnessPicker = (BrightnessPickerView) findViewById(R.id.view_brightness);
        mCameraLayout = (LinearLayout) findViewById(R.id.layout_camera_frame);

        mCurrentColorView = findViewById(R.id.bulb_current_color);
        mDeviceImg = (ImageView) findViewById(R.id.iv_rgb_light);
        mWaitRegView = (WaitRegView) findViewById(R.id.waitregview_rgb);

        mWhiteColorView = findViewById(R.id.bulb_color_white);
        mRedColorView = findViewById(R.id.bulb_color_red);
        mYellowColorView = findViewById(R.id.bulb_color_yellow);
        mGreenColorView = findViewById(R.id.bulb_color_green);
        mLightBlueColorView = findViewById(R.id.bulb_color_light_blue);
        mBlueColorView = findViewById(R.id.bulb_color_blue);
        mPinkColorView = findViewById(R.id.bulb_color_pink);
        mBlackColorView = findViewById(R.id.bulb_color_black);

        mContainer = (FrameLayout) findViewById(R.id.view_container);

        // 取色板上取得颜色
        mColorPicker.setOnColorChangeListener(new OnColorChangeListener() {

            @Override
            public void onColorChange(int color, boolean isTouchUp, boolean isTouchDown) {
                if (!isTouchDown) {
                    isTouch = true;
                }

                if (isTouchUp) {
                    isTouch = false;
                }

                mCurrentColorView.setBackgroundColor(color);

                mBrightnessPicker.setColor(color);

                if (mDevice != null) {
                    // 设置设备颜色
                    sidColoring(color, 0, mDevice.getNetWorkState(), isTouchUp);
                }

            }
        });

        mBrightnessPicker.setOnBrightnessChangeListener(new BrightnessPickerView.OnBrightnessChangeListener() {
            @Override
            public void onBrightnessChange(int color, int brightness, boolean isTouchUp, boolean isTouchDown) {
                if (!isTouchDown) {
                    isTouch = true;
                }

                if (isTouchUp) {
                    isTouch = false;
                }

                // 显示块颜色
                mCurrentColorView.setBackgroundColor(color);

                if (mDevice != null) {
                    // 设置设备颜色
//                    sidColoring(color, 0, mDevice.getNetWorkState(), isTouchUp);

                    mDevice.disColoring(brightness, 6, mDevice.getNetWorkState(), false);
                }
            }
        });

        initData();

        updateUI();

    }

    /**
     * 更新设备图片
     */
    public void updateUI(){
        if (mDevice.getImageCount() == 0) {
            //没拍过照的
            int imageId = mDevice.getImageId();
//            mDeviceImg.setImageResource(imageId);
            mDeviceImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            GlideUtil.loadImageView(mContext,imageId,mDeviceImg);
        } else {
//            Bitmap image = mDevice.getImage();
//            mDeviceImg.setImageBitmap(image);

            mDeviceImg.setScaleType(ImageView.ScaleType.CENTER);
            String imagePath = mDevice.getImagePath();
            GlideUtil.loadImageViewNoCacheDisk(mContext, imagePath, mDeviceImg);

        }

    }

    private void initData() {

        if (mDevice != null) {

            // 初始化当前显示块的颜色
            setCurrentColor();

            // 初始化白色拖动条
            long state = mDevice.getCurrentState();
            int progress = (int) (state >> 48);
            mBrightnessPicker.setBrightness(progress);
            updateUI();

        }
    }

    /**
     * 给固定颜色块设置点击事件
     */
    private void initListener() {
        mWhiteColorView.setOnClickListener(mClick);
        mRedColorView.setOnClickListener(mClick);
        mYellowColorView.setOnClickListener(mClick);
        mGreenColorView.setOnClickListener(mClick);
        mLightBlueColorView.setOnClickListener(mClick);
        mBlueColorView.setOnClickListener(mClick);
        mPinkColorView.setOnClickListener(mClick);
        mBlackColorView.setOnClickListener(mClick);
    }

    private OnClickListener mClick = new OnClickListener() {

        @Override
        public void onClick(View v) {

            int color = 0; // RGB颜色
            long white = 0; // 白色

            CommonUtils.showWaitRegViewOnMainThread(mWaitRegView);

            switch (v.getId()) {
                case R.id.bulb_color_white:
                    color = 0xFFFFFF;
                    white = ((long) 0xFFF) << 48;
                    break;
                case R.id.bulb_color_red:
                    color = 0xFF0000;
                    break;
                case R.id.bulb_color_yellow:
                    color = 0xFF8000;
                    break;
                case R.id.bulb_color_green:
                    color = 0x00FF00;
                    break;
                case R.id.bulb_color_light_blue:
                    color = 0x00FFFF;
                    break;
                case R.id.bulb_color_blue:
                    color = 0x0000FF;
                    break;
                case R.id.bulb_color_pink:
                    color = 0xFF00FF;
                    break;
                case R.id.bulb_color_black:
                    color = 0x000000;
                    break;
            }

            // 设置显示块颜色
            mCurrentColorView.setBackgroundColor(0xff000000 | color);

            // 设置设备颜色
            sidColoring(color, white, EIOT_NETWORK_TYPE.EIOT_NETWORK_TYPE_BOTH, true);
        }
    };

    /**
     * 设置设备颜色
     *
     * @param color    RGB颜色值
     * @param white    白色颜色值
     * @param netState 网络状态(是否是本地设备)
     */
    private void sidColoring(int color, long white, int netState, boolean isTouchUp) {
        if (mDevice != null) {
            // 先把rgb颜色值转换成设备对应的颜色值
            long r = (long) ((((0xff0000 & color) >> 16) * MULTIPLES));
            long g = (long) ((((0xff00 & color) >> 8) * MULTIPLES));
            long b = (long) (((0xff & color) * MULTIPLES));

            Log.e("COLOR", "a: 0x" + Long.toHexString(r));
            Log.e("COLOR", "b: 0x" + Long.toHexString(g));
            Log.e("COLOR", "c: 0x" + Long.toHexString(b));

            long disColor = ((b << 32)) | ((g << 16)) | r;

            disColor = disColor | white;

            Log.e("COLOR", "disColor: 0x" + Long.toHexString(disColor) + ",    white: 0x" + Long.toHexString(white));
            Log.e("COLOR", "float: " + MULTIPLES);
            Log.e("COLOR", "color: 0x" + Integer.toHexString(color));
            // 把转换好的值写入设备寄存器
            mDevice.disColoring(disColor, 0, netState, isTouchUp);
        }
    }

    /**
     * 设置显示块的颜色
     */
    private void setCurrentColor() {
        if (mDevice != null) {
            // 先把从设备中读取的颜色值转换成rgb值
            long state = mDevice.getCurrentState();
            Log.e("COLOR", "callback state: 0x" + Long.toHexString(state));
            int r = (int) ((0xffff & state) / MULTIPLES2);
            int g = (int) ((0xffff & (state >> 16)) / MULTIPLES2);
            int b = (int) ((0xffff & (state >> 32)) / MULTIPLES2);

            Log.e("COLOR", "r: 0x" + Long.toHexString(r));
            Log.e("COLOR", "g: 0x" + Long.toHexString(g));
            Log.e("COLOR", "b: 0x" + Long.toHexString(b));

            int rgb = (r << 16) | (g << 8) | b;
            Log.e("COLOR", "callback rgb: 0x" + Integer.toHexString(rgb));
            mCurrentColorView.setBackgroundColor(0xff000000 | rgb);
            mBrightnessPicker.setColor(0xff000000 | rgb);
        }
    }

    public void setDevice(IOTRgbLight dev) {
        if (dev != null){
            mDevice = dev;
            initData();

            // 当寄存器数据变化时修改显示块颜色和白色拖动条
            mDevice.setRegDataChangeListener(new OnRegDataChangedListener() {

                @Override
                public void onRegDataChange() {
                    UIUtils.postTaskSafely(new Runnable() {

                        @Override
                        public void run() {
                            if (!isTouch) {
                                initData();
                                CommonUtils.hideWaitRegView(mWaitRegView);
                            }
                        }
                    });
                }
            });

            if(mDeviceImg != null){
                mDeviceImg.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDevice.turn(!mDevice.isOn());
                    }
                });
            }
        }

    }



}
